# makefile for Railgun 0.4a (Alpha)
DEVICE=/dev/sdb
PARTITION=/dev/sdb1

# variables
BUILD_NUMBER := $(shell cat build-number.txt)
BUILD_DATE := $(shell date +'%Y%m%d')
#BUILD_DAY := $(shell date +'%d')
#BUILD_MONTH := $(shell date +'%m')
#BUILD_YEAR := $(shell date +'%Y')
# -D__BUILD_MONTH=$(BUILD_MONTH) -D__BUILD_YEAR=$(BUILD_YEAR)#-std=c99 #/usr/local/cross/bin/ #/usr/local/cross/bin/

# tool names
NASM=nasm
CGCC64=x86_64-elf-gcc -D__BUILD_NUMBER=$(BUILD_NUMBER) -D__BUILD_DATE=$(BUILD_DATE) -Wall -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-comment -Wimplicit-function-declaration -m64 -ffreestanding -nostdlib -mcmodel=large -mno-red-zone -mno-3dnow -Werror
CLD64=x86_64-elf-ld

# bootloader
boot64:
	nasm boot/mbr.asm -o bin/mbr.bin
	nasm boot/boot.asm -o bin/boot.bin
	nasm boot/bootme.asm -o bin/bootme.sys

# Railgun64.sys
build64:
	# update the build number
	@echo $$(($$(cat build-number.txt) + 1)) > build-number.txt

	# build the 64 bit AP trampoline stub
	nasm setup/asm/trampoline_64.asm -o tmp/trampoline_64.bin

	# build the kernel image
	# setup code
	nasm -w+error -f elf64 -o tmp/header_64.o setup/asm/kernel_header_64.asm
	nasm -w+error -f elf64 -o tmp/test_isr_64.o setup/asm/test_isr_64.asm
	nasm -w+error -f elf64 -o tmp/IRQ_headers_64.o setup/asm/IRQ_headers_64.asm
	$(CGCC64) -o tmp/kmain_64.o -c setup/kmain_xx.c
	$(CGCC64) -o tmp/print_64.o -c setup/printf_xx.c
	$(CGCC64) -o tmp/memory_bitmap_64.o -c setup/memory_bitmap_64.c
	$(CGCC64) -o tmp/setup_GDT_64.o -c setup/setup_GDT_64.c
	$(CGCC64) -o tmp/setup_IDT_64.o -c setup/setup_IDT_64.c
	$(CGCC64) -o tmp/ISR_setup_64.o -c setup/ISR_setup_64.c
	$(CGCC64) -o tmp/setup_PIC_64.o -c setup/setup_PIC_64.c
	$(CGCC64) -o tmp/setup_PIT_64.o -c setup/setup_PIT_64.c
	$(CGCC64) -o tmp/calibrate_APIC_timer_64.o -c setup/calibrate_APIC_timer_64.c
	$(CGCC64) -o tmp/init_AP_64.o -c setup/init_AP_64.c
	$(CGCC64) -o tmp/setup_system_IRQs_64.o -c setup/setup_system_IRQs_64.c
	$(CGCC64) -o tmp/setup_data_area_64.o -c setup/setup_data_area_64.c
	$(CGCC64) -o tmp/time_setup_64.o -c setup/time_64.c
	$(CGCC64) -o tmp/PCI_enum_64.o -c setup/PCI_64.c

	# kernel code
	nasm -w+error -f elf64 -o tmp/spurious_ISR.o kernel/asm/spurious_ISR_64.asm
	nasm -w+error -f elf64 -o tmp/scheduler_ISR.o kernel/asm/scheduler_ISR_64.asm
	nasm -w+error -f elf64 -o tmp/proc_system_idle_64.o kernel/asm/proc_system_idle_64.asm
	nasm -w+error -f elf64 -o tmp/system_bakery_lock_64.o kernel/asm/spinlock_64.asm
	nasm -w+error -f elf64 -o tmp/exception_isr.o kernel/asm/exception_isr_64.asm
	#$(CGCC64) -o tmp/system_bakery_lock_64.o -c kernel/system_bakery_lock.c
	$(CGCC64) -o tmp/scheduler_functions_64.o -c kernel/scheduler_functions.c
	$(CGCC64) -o tmp/proc_CPU_monitor_64.o -c kernel/proc_CPU_monitor.c
	$(CGCC64) -o tmp/proc_setup_64.o -c kernel/proc_setup.c
	$(CGCC64) -o tmp/proc_cleanup_64.o -c kernel/proc_cleanup.c
	$(CGCC64) -o tmp/scheduler_isr_main_64.o -c kernel/scheduler_isr.c
	$(CGCC64) -o tmp/page_alloc_64.o -c kernel/page_alloc.c
	$(CGCC64) -o tmp/message_handling.o -c kernel/message_handling.c
	$(CGCC64) -o tmp/ACPI_tables.o -c kernel/ACPI_tables.c
	$(CGCC64) -o tmp/system_configuration_table_64.o -c kernel/system_config_table.c
	$(CGCC64) -o tmp/process_control.o -c kernel/process_control.c
	$(CGCC64) -o tmp/system_error.o -c kernel/system_error.c
	$(CGCC64) -o tmp/page_table_control.o -c kernel/page_table_control.c
	$(CGCC64) -o tmp/system_exceptions.o -c kernel/system_exceptions.c

	# klib code
	nasm -w+error -f elf64 -o tmp/klib_entry.o klib/asm/klib_syscall_entry.asm
	nasm -w+error -f elf64 -o tmp/klib_sys_ISR.o klib/asm/klib_ISRs.asm
	$(CGCC64) -o tmp/klib_table.o -c klib/klib_function_group_table.c
	$(CGCC64) -o tmp/klib_proc_threads.o -c klib/klib_proc_threads.c
	$(CGCC64) -o tmp/klib_page_alloc.o -c klib/klib_page_alloc.c
	$(CGCC64) -o tmp/klib_message_passing.o -c klib/klib_message_passing.c
	$(CGCC64) -o tmp/klib_IRQ_control.o -c klib/klib_IRQ_control.c
	$(CGCC64) -o tmp/klib_keyboard_buffer.o -c klib/klib_keyboard_buffer.c
	$(CGCC64) -o tmp/klib_ACPI_tables.o -c klib/klib_ACPI_tables.c
	$(CGCC64) -o tmp/klib_system_timers.o -c klib/klib_system_timers.c
	$(CGCC64) -o tmp/klib_system_resources.o -c klib/klib_system_resources.c
	$(CGCC64) -o tmp/klib_PCIE.o -c klib/klib_PCIE.c
	$(CGCC64) -o tmp/klib_volume_manager.o -c klib/klib_volume_manager.c

	# driver code
	$(CGCC64) -o tmp/PS2KB_main.o -c driver/PS2KB/PS2KB_main.c
	$(CGCC64) -o tmp/HPET_main.o -c driver/HPET/HPET_main.c
	$(CGCC64) -o tmp/AP_main.o -c driver/AP/AP_main.c
	$(CGCC64) -o tmp/ahci_main.o -c driver/AHCI/ahci_main.c
	$(CGCC64) -o tmp/ahci_driver.o -c driver/AHCI/ahci_driver.c
	$(CGCC64) -o tmp/ahci_FAT32.o -c driver/AHCI/ahci_FAT32.c

	# ACDM code
	nasm -w+error -f elf64 -o tmp/ACDM_sub.o driver/ACDM/ACDM_sub.asm
	$(CGCC64) -o tmp/ACDM_buffer.o -c driver/ACDM/ACDM_buffer.c
	$(CGCC64) -o tmp/ACDM_window.o -c driver/ACDM/ACDM_window.c
	$(CGCC64) -o tmp/ACDM_keyboard.o -c driver/ACDM/ACDM_keyboard.c
	$(CGCC64) -o tmp/ACDM_proc.o -c driver/ACDM/ACDM_proc.c
	$(CGCC64) -o tmp/ACDM_mod.o -c driver/ACDM/ACDM_mod.c
	$(CGCC64) -o tmp/ACDM_start.o -c driver/ACDM/ACDM_start.c


	# link it all together
	$(CLD64) -T setup/link_64.ld -o tmp/kernel_64.bin \
	tmp/header_64.o \
	tmp/kmain_64.o \
	tmp/print_64.o \
	tmp/memory_bitmap_64.o \
	tmp/time_setup_64.o \
	tmp/setup_GDT_64.o \
	tmp/setup_IDT_64.o \
	tmp/ISR_setup_64.o \
	tmp/test_isr_64.o \
	tmp/IRQ_headers_64.o \
	tmp/setup_PIC_64.o \
	tmp/setup_PIT_64.o \
	tmp/calibrate_APIC_timer_64.o \
	tmp/system_configuration_table_64.o \
	tmp/setup_system_IRQs_64.o \
	tmp/ACPI_tables.o \
	tmp/spurious_ISR.o \
	tmp/scheduler_ISR.o \
	tmp/init_AP_64.o \
	tmp/setup_data_area_64.o \
	tmp/proc_system_idle_64.o \
	tmp/scheduler_functions_64.o \
	tmp/proc_cleanup_64.o \
	tmp/proc_CPU_monitor_64.o \
	tmp/proc_setup_64.o \
	tmp/system_bakery_lock_64.o \
	tmp/scheduler_isr_main_64.o \
	tmp/page_alloc_64.o \
	tmp/klib_entry.o \
	tmp/klib_table.o \
	tmp/klib_proc_threads.o \
	tmp/klib_page_alloc.o \
	tmp/message_handling.o \
	tmp/klib_message_passing.o \
	tmp/klib_IRQ_control.o \
	tmp/klib_keyboard_buffer.o \
	tmp/PS2KB_main.o \
	tmp/HPET_main.o \
	tmp/klib_ACPI_tables.o \
	tmp/klib_system_timers.o \
	tmp/AP_main.o \
	tmp/ACDM_mod.o \
	tmp/ACDM_start.o \
	tmp/ACDM_proc.o \
	tmp/klib_system_resources.o \
	tmp/ACDM_buffer.o \
	tmp/ACDM_sub.o \
	tmp/ACDM_window.o \
	tmp/ACDM_keyboard.o \
	tmp/process_control.o \
	tmp/system_error.o \
	tmp/page_table_control.o \
	tmp/exception_isr.o \
	tmp/system_exceptions.o \
	tmp/ahci_main.o \
	tmp/PCI_enum_64.o \
	tmp/klib_PCIE.o \
	tmp/klib_sys_ISR.o \
	tmp/ahci_driver.o \
	tmp/klib_volume_manager.o \
	tmp/ahci_FAT32.o

	# stick kernel and trampoline code together
	cat tmp/trampoline_64.bin tmp/kernel_64.bin > bin/Railgun64.sys


# copy everything to the disk
copy: bin/Railgun64.sys

	# put bootsector on disk
	#sudo fat32bsc -d=$(PARTITION) -f=bin/boot.bin
	#sync
	#sudo mbrc -f=bin/mbr.bin -d=$(DEVICE)
	#sync

	# mount the partition
	sudo mkdir /media/Railgun
	sudo mount $(PARTITION) /media/Railgun

	# copy the required files to it
	sudo rm -fR /media/Railgun/RailgunX
	sudo mkdir /media/Railgun/RailgunX
	sudo cp bin/Railgun64.sys /media/Railgun/RailgunX/
	sudo cp bin/bootme.sys /media/Railgun/

	# unmount the partition
	sleep 1
	sudo umount /media/Railgun
	sudo rmdir /media/Railgun

# test
run:
	sudo qemu-system-x86_64 -M q35 \
	 -m 1024 -smp 4 -no-reboot \
	 -device ahci,id=ahci0 \
	 -drive file=$(DEVICE),format=raw,id=drive-sata0-0-0 \
	 -device ide-drive,bus=ahci0.0,drive=drive-sata0-0-0,id=sata0-0-0 \
	 -drive file=/dev/sda,format=raw,id=drive-sata1-0-0 \
	 -device ide-drive,bus=ahci0.1,drive=drive-sata1-0-0,id=sata1-0-0 \
	 #-rtc base="2014-06-18",clock=vm
	 #-D /home/afiretruck/qemu.log -d int,cpu_reset \
	 #-cpu qemu64,+x2apic \
	 # -drive file=$(DEVICE),if=scsi,bus=0

# build all
all: boot64 build64 copy run









